package io;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import http.HttpUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 请求
 *
 * @Author: ycy
 * @CreateDate: 2019/4/28 15:51
 */
public class NoneResIODemo2 {

  public static void main(String[] args) {
    NoneResIODemo2 oneResIODemo2 = new NoneResIODemo2();
    List<String> htttpUrls = oneResIODemo2.getHtttpUrl();
    oneResIODemo2.printOutFile(htttpUrls);
    List<Long> costTime = Lists.newLinkedList();
    int invalid = 0;
    int cnt = 0;
    for (String url : htttpUrls) {
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println(url);
      Long start = System.currentTimeMillis();
//      GsSearchResponse gsSearchResponse = oneResIODemo2.fetch(url);
      Long end = System.currentTimeMillis();
      cnt++;
      long e = 0;
      if ((cnt % 100) == 0) System.out.println("完成 " + cnt);
//      if (gsSearchResponse == null || gsSearchResponse.result == null || gsSearchResponse.result.isEmpty()) {
//        invalid++;
//      } else {
//        e = end - start;
//        costTime.add(e);
//        System.out.println(e + "\t" + url);
//      }
    }

    System.out.println(htttpUrls.size());
    System.out.println(invalid);
    System.out.println(cnt);

    int totalCostTime = 0;
    for (Long aLong : costTime) {
      totalCostTime += aLong;
    }
    System.out.println("总耗时" + totalCostTime + ",  " + costTime.size());

  }

  public void printOutFile(List<String> idNameList) {
    try {
      PrintWriter pw = new PrintWriter(new PrintStream(new FileOutputStream("\\Users\\yincy\\Desktop\\print.txt")));
      for (String alias : idNameList) {
        pw.println(alias);
      }
      pw.flush();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {

    }
  }

  private Pattern pattern = Pattern.compile("\\d+");

  public List<String> getHtttpUrl() {
    List<String> Urls = Lists.newLinkedList();
    File file = new File("D:\\Users\\yincy\\Desktop\\1\\engine.access.log");
    String line = null;
    try {
      FileInputStream fis = new FileInputStream(file);
      InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
      BufferedReader br = new BufferedReader(isr);
      //一行一读
      while (br.ready()) {
        line = br.readLine();
        if (StringUtils.isNotBlank(line)) {
          String[] split = line.split("\t");
          String time = split[0].trim();
          String url = split[1].trim();
          if (url.startsWith("/gssearch/search?action=autocomplete")) {
            try {
              if (Integer.valueOf(time) > 0) {
                //获取 500ms 以上的数据
                Urls.add(url);
              }
            } catch (NumberFormatException e) {
              e.printStackTrace();
            }

          }
        }
      }
    } catch (Exception e) {
      System.out.println(line);
      e.printStackTrace();
    }
    return Urls;
  }

  private static final int MAX_TIME_OUT = 70000;

  public GsSearchResponse fetch(String url) {
    GsSearchResponse gsSearchResponse = null;
    Long startTime = null;
    Long endTime = null;
    try {
      startTime = System.currentTimeMillis();
//      gsSearchResponse = new Gson().fromJson(HttpUtil.sendGet("http://ws.gssearch.search.ctripcorp.com" + url, MAX_TIME_OUT), GsSearchResponse.class);
      gsSearchResponse = new Gson().fromJson(HttpUtil.sendGet("http://globalproxy.search.ctripcorp.com/proxy?url=http://10.25.153.225:8080" + url, MAX_TIME_OUT), GsSearchResponse.class);
      endTime = System.currentTimeMillis();
      System.out.println("[" + sdftime.format(new Date(startTime)) + "]\t" + (endTime - startTime) + "\t" + url);
    } catch (IOException e) {
      endTime = System.currentTimeMillis();
      System.out.println(e.getMessage() + " " + (endTime - startTime) + " " + url);
    }
    return gsSearchResponse;
  }

  class GsSearchResponse {
    public List<Object> result;
    public String resultPageUrl;
  }

  protected static final SimpleDateFormat sdftime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


}
